<?php
class BMSC_Banner extends BMSC_Db
{
	// Thêm thông tin banner vào database
	public function addBanner($bannerTypeID, $media, $mediaType, $url, $target, $priority, $isActive, $createBy)
	{
		try
		{
			$bannerInfo = array();
			$bannerInfo["BANNER_TYPE_ID"] = $bannerTypeID;
			$bannerInfo["MEDIA"] = $media;
			$bannerInfo["MEDIA_TYPE"] = $mediaType;
			$bannerInfo["URL"] = $url;
			$bannerInfo["TARGET"] = $target;
			$bannerInfo["PRIORITY"] = $priority;
			$bannerInfo["IS_ACTIVE"] = $isActive;
			$bannerInfo["CREATE_BY"] = $createBy;
			$bannerInfo["CREATE_DATE"] = date("Y-m-d H:i:s");		
			$bannerInfo["IS_DELETE"] = 0;
			return $this->_writerDb->insert("banner", $bannerInfo);
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}

	// Cập nhật thông tin banner vào database
	public function editBanner($bannerID, $bannerTypeID, $media, $mediaType, $url, $target, $priority, $isActive, $updateBy)
	{
		try
		{
			$bannerInfo = array();
			$bannerInfo["BANNER_TYPE_ID"] = $bannerTypeID;
			if ($media != null)
				$bannerInfo["MEDIA"] = $media;
			if ($mediaType != null)
				$bannerInfo["MEDIA_TYPE"] = $mediaType;
			$bannerInfo["URL"] = $url;
			$bannerInfo["TARGET"] = $target;
			$bannerInfo["PRIORITY"] = $priority;
			$bannerInfo["IS_ACTIVE"] = $isActive;
			$bannerInfo["UPDATE_BY"] = $updateBy;
			$bannerInfo["UPDATE_DATE"] = date("Y-m-d H:i:s");
			return $this->_writerDb->update("banner", $bannerInfo, "ID = " . $bannerID);
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}

	// Xóa thông tin banner khỏi database
	public function removeBanner($bannerID, $updateBy)
	{
		try
		{
			$bannerInfo = array();
			$bannerInfo["IS_DELETE"] = 1;
			$bannerInfo["UPDATE_BY"] = $updateBy;
			$bannerInfo["UPDATE_DATE"] = date("Y-m-d H:i:s");
			return $this->_writerDb->update("banner", $bannerInfo, "ID=" . $bannerID);
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}

	// Lấy danh sách tất cả các loại banner
	public function getBannerTypes()
	{
		try
		{
			$query = sprintf("CALL sp_admin_get_banner_type()");
			$types = $this->_readerDb->fetchAll($query);
			$this->_readerDb->closeConnection();
			return $types;
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}

	// Lấy danh sách tất cả các banner theo loại banner
	public function getBanners($typeID, $from, $numberOfBanners)
	{
		try
		{
			$query = sprintf("CALL sp_admin_get_banner_list(%u, %u, %u)", $typeID, $from, $numberOfBanners);
			$banners = $this->_readerDb->fetchAll($query);
			$this->_readerDb->closeConnection();
			return $banners;
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}

	// Lấy thông tin banner
	public function getBanner($bannerID)
	{
		try
		{
			$query = sprintf("CALL sp_admin_get_banner(%u)", $bannerID);
			$banner = $this->_readerDb->fetchRow($query);
			$this->_readerDb->closeConnection();
			return $banner;
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}
}
?>